﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Data;
using System.Xml;

/// <summary>
/// Summary description for WSPageBase
/// </summary>
public class WSPageBase : PageBase
{

    #region Members
    protected Ko2.Portal.Env _env = new Ko2.Portal.Env();
    string strYear, strMonth;
    int _Year, _Month;
    public int Year
    {
        get { return _Year; }
    }
    public int Month
    {
        get { return _Month; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["Year"] != null)
            strYear = Request.Params["Year"].ToString();
        else
            strYear = DateTime.Now.Year.ToString();

        if (Request.Params["Month"] != null)
            strMonth = Request.Params["Month"].ToString();
        else
            strMonth = DateTime.Now.Month.ToString();

        _Year = Convert.ToInt32(strYear);
        _Month = Convert.ToInt32(strMonth);
    }

    protected Exception MakeXML(ref XmlDocument xReturn, ref DataTable tbl, ref Exception err)
    {
        Exception ret = null;

        if (err != null)
        {
            tbl = new DataTable("ErrorsTbl");

            tbl.Columns.Add("ID", typeof(string));
            tbl.Columns[0].Caption = "ID";
            tbl.Columns.Add("Name", typeof(string));
            tbl.Columns[1].Caption = "Name";

            DataRow r = tbl.NewRow();
            r[0] = "-1000";
            r[1] = "Ошибка получения данных.";
            tbl.Rows.Add(r);
        }

        #region XML stream

        using (StringWriter stringWriter = new StringWriter())
        {
            using (XmlTextWriter xmlTextwriter = new XmlTextWriter(stringWriter))
            {
                tbl.WriteXml(xmlTextwriter, XmlWriteMode.WriteSchema);
                xReturn.LoadXml(stringWriter.ToString());
                XmlNode xErrors = xReturn.CreateElement("Errors");
                if (err != null)
                {
                    XmlNode xErr = xReturn.CreateElement("Err");
                    xErr.InnerText = err.Message;
                    xErrors.AppendChild(xErr);
                }
                xReturn.ChildNodes[0].AppendChild(xErrors);
            }
        }

        #endregion

        return ret;
    }

}